#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCoreApplication>
#include <QtSql>
#include <QApplication>
#include <QSqlDatabase>
#include <cstdlib>
#include <QMovie>
#include <QDateTime>
#include <QLabel>
#include <QMovie>
#include <QSplashScreen>
#include "qtoolbox.h"
#include <synchapi.h>

QString username;
QString password;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    QPixmap pix(":/resource/splash2.gif");
        QSplashScreen splash(pix);
        QLabel splashlabel(&splash);
        QMovie splashgif(":/start_game.gif");
        splashlabel.setMovie(&splashgif);
        splashgif.start();
        splash.show();
        splash.setCursor(Qt::BlankCursor);//不显示鼠标外观
        for(int i=0;i<15000;i+=splashgif.speed()){
            QCoreApplication::processEvents();
            Sleep(splashgif.speed()/5);
        }
    splash.finish(this);
    // 获取用户输入的账号
       bool ok1;
       username = QInputDialog::getText(nullptr, "登录", "请输入账号:", QLineEdit::Normal, "", &ok1);
       if (ok1) {
           // 获取用户输入的密码
           bool ok2;
           password = QInputDialog::getText(nullptr, "登录", "请输入密码:", QLineEdit::Password, "", &ok2);
           if (ok2) {
               // 建立与数据库的连接
               QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
               db.setHostName("127.0.0.1");
               db.setPort(3306);
               db.setDatabaseName("cnm");
               db.setUserName("root");
               db.setPassword("123456");
               if (db.open()) {
                   // 查询数据库中是否存在匹配的账号密码
                   QSqlQuery query;
                   query.prepare("SELECT * FROM users WHERE username = :username AND password = :password");
                   query.bindValue(":username", username);
                   query.bindValue(":password", password);

                   if (query.exec() && query.next()) {
                       // 账号密码正确，继续执行
                       ui->setupUi(this);
                       cms = nullptr;
                       ams = nullptr;
                       setWindowTitle("贪吃蛇");
                       // 在这里可以继续执行其他操作
                       //Init();
                   } else {
                       // 账号密码不正确
                       qDebug() << "账号密码错误。";
                       QMessageBox::critical(nullptr, "错误", "账号密码错误。");
                       std::exit(0);
                   }

                   // 关闭数据库连接
                   db.close();
               } else {
                   qDebug() << "Failed to connect to database.";
               }
           } else {
               // 用户取消输入密码
               qDebug() << "取消输入密码。";
               std::exit(0);
           }
       } else {
           // 用户取消输入账号
           qDebug() << "取消输入账号。";
           std::exit(0);
       }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::Init()
{
    QMovie *movie=new QMovie(":/start_game.gif");
    movie->start();
    ui->label->setMovie(movie);
    ui->label->setScaledContents(true);
}


//经典模式
void MainWindow::on_classicalModeBtn_clicked()
{
    this->hide();
    cms = new CMSnake(username,password);
	QObject::connect(cms, SIGNAL(showMainWindow()), this, SLOT(receiveShowMainWindow()));
	if(!cms->play())
		receiveShowMainWindow();
}

//自动贪吃蛇
void MainWindow::on_autoModeButton_clicked()
{
	this->hide();
	ams = new AMSnake;
	QObject::connect(ams, SIGNAL(showMainWindow()), this, SLOT(receiveShowMainWindow()));
	if(!ams->play())
		receiveShowMainWindow();
}

//接受showMainWindow信号
void MainWindow::receiveShowMainWindow()
{
	if(cms != nullptr) {delete cms; cms = nullptr;}
	if(ams != nullptr) {delete ams; ams = nullptr;}
	this->show();
}

void MainWindow::on_pushButton_clicked()
{
    b.show();
}
